package com.kobeliu.entity;

public class Demo_11 {

    public static void main(String[] args) {
        int[] nums1 = {1,2};
        int[] nums2 = {3};

        System.out.println(findMedianSortedArrays(nums1,nums2));
    }

    public static double findMedianSortedArrays(int[] nums1, int[] nums2) {
        double d = 0.00000;
        int n = nums1.length+nums2.length;
        if(n==0){
            return d;
        }
        if(n==1){
            if(nums1.length==1){
                return (double)nums1[0];
            }else{
                return (double)nums2[0];
            }
        }

        int[] help = new int[n];
        int L = 0; //nums1数组的指针
        int R = 0; //nums2数组的指针
        int temp = 0; //help数组的指针

        while(L < nums1.length && R < nums2.length){
            help[temp++] = nums1[L]<nums2[R]? nums1[L++]:nums2[R++];
        }
        while(L <nums1.length){
            help[temp++] = nums1[L++];
        }
        while(R <nums2.length){
            help[temp++] = nums2[R++];
        }

        if(temp%2 != 0){
            return (double)help[(temp-1)/2];
        }
        //求help数组中间两个数
        int mid = (temp-1)/2;

        d = (double)(help[mid] + help[mid+1])/2;

        return d;

    }
}
